package com.example.project.controller.merchant;

import com.example.project.common.Result;
import com.example.project.entity.Category;
import com.example.project.service.CategoryService;
import com.example.project.vo.CategoryTreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/merchant/categories")
public class MerchantCategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping
    public Result<List<Category>> list() {
        List<Category> categories = categoryService.getEnabledCategories();
        return Result.success(categories);
    }
    
    @GetMapping("/tree")
    public Result<List<CategoryTreeVO>> tree() {
        List<CategoryTreeVO> tree = categoryService.getCategoryTree();
        return Result.success(tree);
    }
    
    @GetMapping("/first-level")
    public Result<List<Category>> firstLevel() {
        List<Category> categories = categoryService.getFirstLevelCategories();
        return Result.success(categories);
    }
    
    @GetMapping("/second-level/{parentId}")
    public Result<List<Category>> secondLevel(@PathVariable Long parentId) {
        List<Category> categories = categoryService.getSecondLevelCategories(parentId);
        return Result.success(categories);
    }
    
    @PostMapping
    public Result<?> add(@RequestBody Category category) {
        return categoryService.addCategory(category);
    }
    
    @PutMapping("/{id}")
    public Result<?> update(@PathVariable Long id, @RequestBody Category category) {
        category.setId(id);
        return categoryService.updateCategory(category);
    }
    
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        return categoryService.deleteCategory(id);
    }
    
    @PutMapping("/{id}/status")
    public Result<?> toggleStatus(@PathVariable Long id, @RequestParam Integer status) {
        return categoryService.toggleStatus(id, status);
    }
    
    @PutMapping("/{id}/sort")
    public Result<?> updateSort(@PathVariable Long id, @RequestParam Integer sort) {
        return categoryService.updateSort(id, sort);
    }
} 